#include "WatermarkManager.h"
#include "log/log.h"
#include "ProductWatermark.h"
#include "WMarkConfig.h"
namespace android
{

    WatermarkManager *WatermarkManager::mSingleton = NULL;

    WatermarkManager *WatermarkManager::getInstance()
    {
        if (mSingleton == NULL)
        {
            mSingleton = new WatermarkManager();
        }
        return mSingleton;
    }

    WatermarkManager::WatermarkManager()
    {
        int queue_size = 15;
        WMarkConfig *wmarkConfig = WMarkConfig::getInstance();
        mDisplayX = wmarkConfig->getDisplayPositionX();
        mDisplayY = wmarkConfig->getDisplayPositionY();

        mBufferQueue = new BufferQueue<WMarkBuffer>(queue_size);
        mReader = new ProductWatermark(this, mBufferQueue);
        mDispatcher = new DispatcherThread(this, mBufferQueue);
    }

    void WatermarkManager::notifyDataReady()
    {
        if (mDispatcher != NULL)
        {
            mDispatcher->onDataReady();
        }
    }

    void WatermarkManager::startInput()
    {
        if (mDispatcher == NULL || mReader == NULL)
        {
            ALOGE("%s %d obj=NULL\n", __func__, __LINE__);
            return;
        }
        if (WMarkConfig::getInstance()->isEnableWatermark()) {
            mDispatcher->start();
            mReader->start();
        }
    }

    void WatermarkManager::stopInput()
    {
        if (mDispatcher == NULL || mReader == NULL)
        {
            ALOGE("%s %d obj=NULL\n", __func__, __LINE__);
            return;
        }
        mReader->stop();
        mDispatcher->stop();
    }

    void WatermarkManager::onDispatcherResult(WMarkBuffer *wMarkBuf) {
        if (!mResultCallback) {
            ALOGW("mResultCallback == NULL, no return watermark result.\n");
            return;
        }
        WaterMarkImage result;
        result.h = wMarkBuf->getDataHeight();
        result.w = wMarkBuf->getDataWidth();
        result.display_x = mDisplayX;
        result.display_y = mDisplayY;
        result.image = wMarkBuf->Data();
        mResultCallback->onWaterMarkResult(result);
    }

}